package com.xnyzc.lhy.mis.service.impl.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.CouponException;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.coupon.CouponInfoSearchResult;
import com.xnyzc.lhy.mis.entity.coupon.CouponPublishParam;
import com.xnyzc.lhy.mis.entity.coupon.OaSysCouponInfo;
import com.xnyzc.lhy.mis.mapper.coupon.OaSysCouponInfoMapper;
import com.xnyzc.lhy.mis.service.coupon.IOaSysCouponInfoService;
import org.apache.commons.codec.language.bm.Lang;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@Service
public class OaSysCouponInfoServiceImpl extends ServiceImpl<OaSysCouponInfoMapper, OaSysCouponInfo> implements IOaSysCouponInfoService {
    @Autowired
    OaSysCouponInfoMapper oaSysCouponInfoMapper;

    /**
     * 获取优惠券发放记录
     *
     * @return
     */
    @Override
    public List<CouponInfoSearchResult> queryCouponInfoList(String batchId) {
        if(StringUtils.isEmpty(batchId))
        {
            throw new CouponException(CouponErrorCode.paramError);
        }

        //查询优惠券信息
        QueryWrapper<OaSysCouponInfo> oaSysCouponInfoWrapper = new QueryWrapper<OaSysCouponInfo>();

        //优惠券批次ID
        oaSysCouponInfoWrapper.eq(OaSysCouponInfo.BATCH_ID, batchId);

        //正常记录 =0
        oaSysCouponInfoWrapper.eq(OaSysCouponInfo.DELETED, 0);

        //查询数据
        List<OaSysCouponInfo> oaSysCouponInfoList = oaSysCouponInfoMapper.selectList(oaSysCouponInfoWrapper);

        return this.createCouponInfoSearchResult(oaSysCouponInfoList);
    }

    /**
     * 新增优惠券定义
     *
     * @return
     */
    @Override
    public void createCouponInfo(Long groupId, CouponPublishParam couponPublishParam, String isPublish, Date publishDate) {
        //获取优惠券数据
        Date createDate = new Date();
        OaSysCouponInfo oaSysCouponInfo = new OaSysCouponInfo();
        //优惠券批次ID
        oaSysCouponInfo.setBatchId(Long.parseLong(couponPublishParam.getBatchId()));
        //优惠券分组ID
        oaSysCouponInfo.setGroupId(groupId);
        //乘客手机号
        oaSysCouponInfo.setPassengerPhone(couponPublishParam.getPassengerPhone());
        //工单ID
        if(StringUtils.isNoneEmpty(couponPublishParam.getWorksheetId())) {
            oaSysCouponInfo.setWorksheetId(Long.parseLong(couponPublishParam.getWorksheetId()));
        }
        //发放张数
        oaSysCouponInfo.setAmount(Integer.parseInt(couponPublishParam.getPublishAmount()));
        //是否发放
        if(StringUtils.isNoneEmpty(isPublish)) {
            oaSysCouponInfo.setIsPublish(Integer.parseInt(isPublish));
        }
        //发放时间
        if(publishDate != null) {
            oaSysCouponInfo.setPublishTime(publishDate);
        }

        Long infoId = IDUtil.nextId();
        oaSysCouponInfo.setId(infoId);
        oaSysCouponInfo.setCreateTime(createDate);
        oaSysCouponInfo.setCreateUser(TokenUtil.getTokenUserId());
        oaSysCouponInfo.setDeleted(0);
        oaSysCouponInfo.setVersion(0L);
        oaSysCouponInfo.setUpdateTime(createDate);

        oaSysCouponInfoMapper.insert(oaSysCouponInfo);
    }

    /**
     * 创建查询返回数据
     * @param oaSysCouponInfoList
     * @return
     */
    private List<CouponInfoSearchResult> createCouponInfoSearchResult(List<OaSysCouponInfo> oaSysCouponInfoList) {
        if(oaSysCouponInfoList !=null
                && oaSysCouponInfoList.size() >0){
            List<CouponInfoSearchResult> couponInfoSearchResultList = new ArrayList<CouponInfoSearchResult>(oaSysCouponInfoList.size());
            for (OaSysCouponInfo oaSysCouponInfo : oaSysCouponInfoList) {
                CouponInfoSearchResult couponInfoSearchResult = new CouponInfoSearchResult();

                //ID
                couponInfoSearchResult.setId(oaSysCouponInfo.getId().toString());
                //优惠券批次ID
                couponInfoSearchResult.setBatchId(oaSysCouponInfo.getBatchId().toString());
                //乘客手机号
                couponInfoSearchResult.setPassengerPhone(oaSysCouponInfo.getPassengerPhone());
                //发放张数
                if(oaSysCouponInfo.getAmount() != null) {
                    couponInfoSearchResult.setAmount(oaSysCouponInfo.getAmount().toString());
                }
                //发放时间
                if(oaSysCouponInfo.getPublishTime() != null) {
                    couponInfoSearchResult.setPublishTime(oaSysCouponInfo.getPublishTime().toString());
                }

                couponInfoSearchResultList.add(couponInfoSearchResult);
            }

            return couponInfoSearchResultList;
        }
        else{
            return null;
        }
    }
}
